package com.zhku.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @BelongsProject: SpringBoot_School_Work
 * @BelongsPackage: com.zhku.controller
 * @Author: "deweyMDH"
 * @CreateTime: 2023-04-03  18:07
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {

    // 测试上传请求
    @RequestMapping(value = "/upload")
    public String upload(MultipartFile file) throws IOException {
        // 1、随机数  2、时间戳  3UUID
        String uuid = UUID.randomUUID().toString();
        // getOriginalFilename ---文件名
        String filename = file.getOriginalFilename();
        // 后缀
        String subfix = filename.substring(filename.lastIndexOf("."));
        // 拼接uuid，和后缀名，组成存入数据库的数据
        filename = uuid + subfix;
        System.out.println("文件名： " + filename);
        // 文件上传
        file.transferTo(new File("D:\\study_PhotoAndWord02\\校企合作资料\\file\\" + filename));
        // 返回图片访问的url
        return "http://localhost:8083/" + filename;
    }
}
